package com.mes.handler;

import java.util.Map;
import org.codehaus.jettison.json.JSONObject;
import org.openbravo.client.kernel.BaseActionHandler;
import org.openbravo.dal.service.OBDal;

//作业订单查看产品图纸
public class ViewmaterialOrderHandler extends BaseActionHandler {
	@Override
	protected JSONObject execute(Map<String, Object> parameters, String data) {
		JSONObject jsonRep = new JSONObject();
		try {
			jsonRep.put("flag", true);
			JSONObject jsonData = new JSONObject(data);
			String resultId = jsonData.getString("id");
			String processId = jsonData.getString("processId");
			String sql = null;
			if (processId != null && !"".equals(processId)) {
				sql = "select productdrawingpath from m_product p "
						+ "where exists(select 1 from emes_process_route pr, emes_process p "
						+ "where p.m_product_id = pr.m_product_id and p.emes_process_route_id = p.emes_process_route_id "
						+ "  and p.emes_process_id = ?)";
				resultId = processId;
			} else {
				sql = "select Productdrawingpath from m_product where m_product_id = "
						+ "(select m_product_id from Emeso_ManuOrder_Job_Order where code = ?)";
			}
			String result = (String) OBDal.getInstance().getSession().createSQLQuery(sql).setString(0, resultId)
					.uniqueResult();
			if (result == null) {
				jsonRep.put("message", "该工序没有上传产品图纸！！");
				jsonRep.put("flag", false);
			} else {
				jsonRep.put("meg", result);
				jsonRep.put("flag", true);
			}
			OBDal.getInstance().commitAndClose();
		} catch (Exception e) {
			OBDal.getInstance().rollbackAndClose();
			e.printStackTrace();
		}
		return jsonRep;
	}
}
